var setting = {
		callback: {
			onClick: zTreeOnClick
		},
        view: {
            showIcon:false,
            selectedMulti:false
        },
        data: {
        	key: {
    			name: "typeName"
    		},
            simpleData: {
                enable: true,
                idKey: "id",
    			pIdKey: "parentId",
    			rootPId: 0
            }
        }
    };

 $(function(){
	//物料类型树
	 $.get($('body').attr('ctxPath') +'/materielType/getMaterielType?t='+new Date().getTime(),function(data){
			if(data.state){
				var treeData=data.obj;
				$.fn.zTree.init($("#treeDemo"),setting,treeData);
			}
		});
	 
	//按钮绑定
	$("#add").bind("click",add);//点击新增
	$("#remove").bind("click", remove);//点击删除
	$("#edit").bind("click",edit);//点击修改
	$("#save").bind("click",addTree);//新增保存
	$('#upate').bind("click",upate);//编辑保存
});

//树节点 点击事件
 function zTreeOnClick(event, treeId, treeNode) {
	 	$('#long_number').val(treeNode.longCode);
	 	$('#add_type_code').val(treeNode.typeCode);
	 	$('#add_type_id').val(treeNode.id);
	 	$("#editTypeCode").val(treeNode.typeCode)
     	listui.refresh('mainUI');
 }
 
//点击物料类型新增按钮
 function add() {
	 $('#addMaterielTypeModal :input').val('');//清空输入框
	 $('#addMaterielTypeModal').modal({'moveable':true,'show': true});
 };

 //点击新增保存按钮
 function addTree() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getSelectedNodes(),
	treeNode = nodes[0];
	var materielTypeCode = $('#addTypeCode').val();
	
	if(treeNode==null){//无选择类型,添加的节点为父节点
 		$('#addLongCode').val(materielTypeCode);
 		$('#parentId').val('0');
 	}else{//添加的是子节点
 		$('#addLongCode').val(treeNode.longCode+"!"+materielTypeCode);
 		$('#parentId').val(treeNode.id);
 	}
	
 	$.post($('body').attr('ctxPath') +'/materielType/addMaterielType?t='+new Date().getTime(),$('#materielTypeForm').serialize(),function(data){
 		if(data.state){
 			var obj=data.obj;
 			$('#addMaterielTypeModal').modal('toggle', 'center');
 			layer.msg(data.msg);
 			treeNode = zTree.addNodes(treeNode, {'id':obj.id,"typeCode":obj.typeCode,"parentId":obj.parentId,"typeName":obj.typeName,"longCode":obj.longCode,'isParent':false});
 			return;
 		}else{	
 			layer.msg(data.msg);
 			return;
 		}
 	});
 }
 
 //点击物料类型编辑
 function edit() {
 	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
 	nodes = zTree.getSelectedNodes(),
 	treeNode = nodes[0];
 	var id = treeNode.id;
 	
 	if (nodes.length == 0) {
 		layer.msg("请先选择物料类型");     
 		return;
 	}
 	
 	//校验当前节点是否被引用
// 	$.post($('body').attr('ctxPath') +'/materielType/hasMateriel/'+id+'?_t='+new Date().getTime(),function(data){//id是当前选择节点id.物料类型ID
// 		if(data.flag){
 //			layer.alert(data.msg);
 //			return;
 //		}else{
 			$('#id').val(treeNode.id);
 		 	$('#editTypeName').val(treeNode.typeName);
 		 	
 		 	$('#editMaterielTypeModal').modal({
 		 		moveable:true,
 		 		position: 100,
 		 		show: true
 		 	});
// 		}
 //	});
 };

 //编辑保存
 function upate() {
	 var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	 	nodes = zTree.getSelectedNodes(),
	 	treeNode = nodes[0];
 	$.post($('body').attr('ctxPath') +'/materielType/editMaterielType?t='+new Date().getTime(),$('#editMaterielTypeForm').serialize(),function(data){
 		
 		if(data.state){
 			treeNode.typeName=$('#editTypeName').val();
 			$('#editMaterielTypeModal').modal('toggle', 'center');
 			zTree.updateNode(treeNode);
 			layer.msg(data.msg)
 			return;
 		}else{
 			layer.msg(data.msg);
 			return;
 		}
 	});
 }
 //点击物料类型删除
 function remove() {
 	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
 	var nodes = zTree.getSelectedNodes();
 	var treeNode = nodes[0];
 	var id = treeNode.id;
 	
 	if (nodes.length == 0) {
 		layer.msg( '请先选择物料类型');
 		return;
 	}
 	//校验当前节点是否被引用
 	$.post($('body').attr('ctxPath') +'/materielType/hasMateriel/'+id+'?_t='+new Date().getTime(),function(data){//id是当前选择节点id.物料类型ID
 		if(data.flag){
 			layer.alert(data.msg);
 			return;
 		}else{
 		layer.confirm('确定删除"'+treeNode.typeName+'"吗', {btn: ['确认','取消']}, function(){
			 	$.post($('body').attr('ctxPath') +'/materielType/deleteMaterielType/'+id+'?t='+new Date().getTime(),function(data){
			 		if(data.state){
			 			zTree.removeNode(nodes[0]);
			 			layer.msg(data.msg);
			 			return;
			 		}else{
			 			llayer.msg(data.msg);
			 			return;
			 		}
			 	});
			 	});
 			}
 		});
};
